<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">
          <img src="https://likede2-admin.itheima.net/img/logo.595745bd.png" alt />
        </h3>
      </div>
      <el-form-item prop="loginName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input v-model="loginForm.loginName" type="text" placeholder="请输入账号" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input ref="inps" v-model="loginForm.password" :type="inpType" placeholder="123456" />
        <span class="svg-container">
          <svg-icon :icon-class="inpType === 'password' ? 'eye' :'eye-open'" @click="eyeFn" />
        </span>
      </el-form-item>
      <el-form-item prop="code">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input v-model="loginForm.code" type="text" placeholder="请输入验证码" />
        <p class="pic" @click="changeImg">
          <img :src="imgUrl" alt />
        </p>
      </el-form-item>
      <el-button
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:30px;"
        @click="handleLogin"
      >登录</el-button>
      <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span>password: any</span>
      </div>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'Login',
  data() {
    return {
      inpType: 'password',
      loginForm: {
        loginName:'admin',
        password:'admin'
      },
      loginRules: {
        loginName: [{ required: true, message: '账号必填', trigger: 'blur' }],
        password: [
          { required: true, message: '密码必填', trigger: 'blur' },
          { min: 2, max: 11, message: '请输入5-11位', trigger: 'blur' },
        ],
        code: [{ required: true, message: '验证码必填', trigger: 'blur' }],
      },
      loading: false,
      yzm: '',
      imgUrl: '',
      clientToken:1,
    }
  },
  mounted() {
    this.getImg()
  },
  methods: {
    async handleLogin() {
      try {
        await this.$refs.loginForm.validate()
        this.loading = true
        this.$store.dispatch('user/loginAciotns', {
          loginName: this.loginForm.loginName,
          password: this.loginForm.password,
          code: this.loginForm.code,
          clientToken: this.clientToken,
          loginType: '0',
        })
        this.$router.push('/')
      } catch (err) {
        console.log(err)
      } finally {
        this.loading = false
      }
    },
    getRandom() {
      this.clientToken = Math.floor(Math.random() * 100)
    },
    async getImg() {
      // 定义的随机数
      this.getRandom()
      const res = await axios({
        url: `https://likede2-admin.itheima.net/likede/api/user-service/user/imageCode/${this.clientToken}`,
        responseType: 'arraybuffer',
      })
      const { data } = res
      const bufferUrl = btoa(
        new Uint8Array(data).reduce(
          (data, byte) => data + String.fromCharCode(byte),
          ''
        )
      )
      const imgUrl = 'data:image/png;base64,' + bufferUrl
      this.imgUrl = imgUrl
    },
    //改变图片
    changeImg(){
       this.getRandom()
       this.getImg()
    },
    eyeFn() {
      this.inpType === 'password'
        ? (this.inpType = 'text')
        : (this.inpType = 'password')
      this.$nextTick(() => {
        this.$refs.inps.focus()
      })
    },
  },
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .title-container {
    transform: translateY(-65px);
    h3 {
      width: 96px;
      height: 96px;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #333;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #ccc;
    background: #fff;
    border-radius: 5px;
    color: #454545;
    transform: translateY(-20px);
    margin-bottom: 40px;
  }
  .el-form-item:nth-child(4) {
    p {
      display: block;
      position: absolute;
      top: -14px;
      right: 0;
      width: 140x;
      height: 53px;
      // background-color: pink;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-image: url('https://likede2-admin.itheima.net/img/background.be4fae7d.png');
  overflow: hidden;

  .login-form {
    position: relative;
    width: 518px;
    max-width: 100%;
    padding: 20px 35px 0;
    margin: 200px auto 100px;
    background-color: #fff;
    border-radius: 10px;
    // border: 1px solid #fff;
    // overflow: hidden;
    z-index: 100;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
